package com.example.demo.business.service;

import com.example.demo.base.bean.Meta;
import com.example.demo.base.exceptions.BaseException;
import com.example.demo.business.bean.entity.HStar;
import com.example.demo.business.bean.entity.HVideo;
import com.example.demo.business.bean.vo.StarDetailVo;
import com.example.demo.business.bean.vo.VideoItemVo;
import com.example.demo.business.dao.HStarDao;
import com.example.demo.business.dao.HVideoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

@Service
public class StarService {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private HStarDao starDao;
    @Autowired
    private HVideoDao hVideoDao;

    public StarDetailVo getStarDetail(String token, int starId, Pageable pageable){
        HStar hStar = starDao.findById(starId).orElseThrow(()->new BaseException(Meta.StarNOTExist));

        int uid = tokenService.getUidByToken(token);
        //TODO 判断是否收藏
        StarDetailVo starDetailVo = new StarDetailVo();

        Page<HVideo> list = hVideoDao.findByStarsLike("%"+starId+"%",pageable);
        starDetailVo.setCollection(false);
        starDetailVo.setName(hStar.getName());
        starDetailVo.setImg(hStar.getImg());
        starDetailVo.setMovieNum(20);
        starDetailVo.setPopular(1000);
        starDetailVo.setStarId(starId);
        starDetailVo.setMovieList(list.getContent().stream().map(item->{
            VideoItemVo videoItemVo = new VideoItemVo();
            videoItemVo.setName(item.getName());
            videoItemVo.setCollection(true);
            videoItemVo.setHot(item.getHot());
            videoItemVo.setPlayTime(item.getPlayTime());
            videoItemVo.setVip(item.getVip()!=0);
            videoItemVo.setImg(item.getShowImg());
            videoItemVo.setDownUrl(item.getNormalVideoUrls());
            videoItemVo.setType(item.getType());
            videoItemVo.setIds(String.valueOf(item.getId()));
            return videoItemVo;
        }).collect(Collectors.toList()));
        return starDetailVo;
    }
}
